package com.eric.windows.imgDispose;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import java.util.Random;

/**
 * <h3>Description</h3>
 * TODO
 * <h3>Author</h3> Eric
 * <h3>Date</h3> 2017/8/18 11:05
 * <h3>Copyright</h3> Copyright (c)2017 Shenzhen TLIT  Co., Ltd. Inc. All rights reserved.
 */
public class GameView extends View {

    private int racketY;
    private final  int RACKET_HEIGHT = 20;
    private final  int RACKET_WIDTH = 20;
    private final int BALL_SIZE = 12;
    Random rand = new Random();
    private int ballX = rand.nextInt(200) + 20;
    private int ballY = rand.nextInt(10) + 20;
    private int racketX = rand.nextInt(200);
    public boolean isCLose = false;


    Paint paint = new Paint();
    public GameView(Context context) {
        super(context);
        initData();
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initData();

    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData();

    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public GameView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initData();
    }

    void initData(){
        paint.setAntiAlias(true);
        setLayerType(View.LAYER_TYPE_SOFTWARE,null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isCLose){
            paint.setColor(Color.RED);
            paint.setTextSize(40);
            canvas.drawText("游戏结束",50,200,paint);
        }else {
            paint.setColor(Color.rgb(240,240,80));
            canvas.drawCircle(ballX,ballY,BALL_SIZE,paint);
            paint.setColor(Color.rgb(80,80,200));
            canvas.drawRect(racketX,racketY,racketX + RACKET_WIDTH,racketY + RACKET_HEIGHT,paint);
        }
    }

}